package com.lagou.dubbo.filter;

import com.lagou.dubbo.common.MonitorHelper;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

@Activate(group = {CommonConstants.CONSUMER}, value = "TPMonitorFilter")
public class TPMonitorFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

        long startTime = System.currentTimeMillis();

        try {
            return invoker.invoke(invocation);
        } finally {
            long executeTime = System.currentTimeMillis() - startTime;

            // 统计tp信息
            MonitorHelper.countTP(invocation.getMethodName(), executeTime);
        }
    }
}
